package com.comptel.actions.service;

import java.util.Collections;
import java.util.Map;

import com.comptel.actions.Action;
import com.comptel.actions.ActionProvider;
import com.comptel.actions.ActionResult;
import com.comptel.actions.discovery.DiscoveryMethod;

public class ActionBean implements ActionProvider {
	
	static Map<String, ActionProvider> providers;
		
	private DiscoveryMethod discoveryMethod;

	@Override
	public String getProviderName() {
		return null;
	}

	@Override
	public ActionResult runAction(Action action, Object obj) {
		ActionProvider ap = providers.get(action.getProviderName());
		return ap.runAction(action, obj);		
	}

	@Override
	public Iterable<Action> getActions(Object dobj) {
		Iterable<Action> actions = Collections.emptyList();
		for (String key : providers.keySet()) {	
			ActionProvider ap = providers.get(key);
			actions = ap.getActions(dobj);
			for (Action act : actions) {
				actions = ap.getActions(dobj);
				break;
			}
			actions = Collections.emptyList();
		}
		return actions;
	}

	public DiscoveryMethod getDiscoveryMethod() {
		return discoveryMethod;
	}

	public void setDiscoveryMethod(DiscoveryMethod discoveryMethod) {
		this.discoveryMethod = discoveryMethod;
		providers = discoveryMethod.discover();
	}

}
